package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.repository.StrategyRepositoey;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepositoey strategyRepositoey;
    @Autowired
    private StrategyThemeServiceImpl strategyThemeService;
    @Autowired
    private StrategyCatalogServiceImpl strategyCatalogService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;
    private Pageable pageable;

    @Override
    public List<Strategy> list() {
        return strategyRepositoey.findAll();
    }

    @Override
    public Strategy selectOne(String id) {
        return strategyRepositoey.findById(id).orElse(null);
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //创建查询对象
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if(StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        //页面显示集合
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Strategy.class, query, pageable);

    }


    @Override
    public void saveOrUpdate(Strategy strategy) {
      /*  private String destId;
        private String destName;目的地名字
        private String themeName;主题的名字
        private String catalogName;分类的名字
        private int isabroad = ABROAD_NO;  //是否是国外*/
      //新增时,这些数据也要添加进数据库
        //主题的名字
        StrategyCatalog strategyCatalog = strategyCatalogService.selectOne(strategy.getCatalogId());
        strategy.setThemeName(strategyThemeService.selectOne(strategy.getThemeId()).getName());
        //分类的名字
        strategy.setCatalogName(strategyCatalog.getName());
        //目的地名字
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());
        //是否在国外
        List<Destination> toast = destinationService.getToast(strategyCatalog.getDestId());
        if (toast!=null && toast.size()>0){
            //集合中第一个就是最大的,正则表达式
            strategy.setIsabroad("中国".equals(toast.get(0).getName())?Strategy.ABROAD_NO:Strategy.ABROAD_YES);
        }
        if((StringUtils.hasLength(strategy.getId()))){
            //有id表示是更新,实现部分更新
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(strategy.getId()));
            Update update =  new Update();
            DBHelper.update(template,query,strategy,"destId","destName","themeId","themeName",
                    "catalogId","catalogName","title","subTitle","summary","coverUrl","isabroad ","state","content");
        }else{
            strategy.setCreateTime(new Date());
            strategyRepositoey.save(strategy);
        }

    }

    @Override
    public void delete(String id) {
        strategyRepositoey.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepositoey.save(strategy);
    }

    @Override
    public void changeState(int state, String id) {
        //部分更新
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> selectByCatalogId(String id) {
       return strategyRepositoey.findByCatalogId(id);
    }


    @Override
    public List<Strategy> queryViewnumTop3ByDestId(String destId) {
        //根据目的地id查询点击量前三的攻略,根据点击量倒序,跳过0个查三个
        Pageable pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
        List<Strategy> strategies= strategyRepositoey.findByDestId(destId,pageable);
        return strategies;

    }

    @Override
    public Strategy get(String id) {

        return this.selectOne(id);
    }

    @Override
    public List<Strategy> findByDestName(String keyword) {

        return strategyRepositoey.findByDestName(keyword);
    }

    @Override
    public List<Strategy> selectByIds(List sids) {
        return strategyRepositoey.findByIdIn(sids);
    }

}

